package com.mach4.gamesuite;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class TicTacToeActivity extends Activity 
{
    ProgressDialog mProgressDialog;
    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tictactoe_board);

        mProgressDialog = ProgressDialog.show(this, "", "Waiting for opponent...", true, true,
            new OnCancelListener()
            {
                @Override
                public void onCancel(DialogInterface dialog)
                {
                    TicTacToeManager.getInstance().onBackKeyPress();
                    finish();
                }
            }
        );
        
        TicTacToeManager.getInstance().setBoardUI(this);
        
        //turn off sleep mode during the game play so that we could keep internet connection
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }  
    
    public void initBoard()
    {
        if (mProgressDialog != null)
            mProgressDialog.dismiss();
    }
    
    public synchronized void showFinalDialog(String title, String message)
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle(title);
        alert.setMessage(message);
        alert.setNeutralButton("OK", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                finish();
            }
        });
        
        alert.show();
    }
    
    public void onButtonPressed(View v)
    {
        Button button = (Button)v;
        String buttonTag = (String)button.getTag();
        
        int buttonId = Integer.parseInt(buttonTag);
        int posRow = buttonId / 3;
        int posCol = buttonId % 3;
        
        TicTacToeManager.getInstance().handleMyMove(posRow, posCol);
    }
        
    public void drawMove(final int posRow, final int posCol, final boolean isGameOwner)
    {
        int oppButtonNumber = 3 * posRow + posCol + 1;
        int id = getResources().getIdentifier("button" + String.valueOf(oppButtonNumber), "id", getPackageName());
        Button pressedButton = (Button)findViewById(id);
        
        String pawn = (isGameOwner)? "O" : "X";
        pressedButton.setText(pawn);
        pressedButton.invalidate();
    }
    
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {   
        if (keyCode != KeyEvent.KEYCODE_BACK) 
            return false;
        
        TicTacToeManager.getInstance().onBackKeyPress();
        return true;    
    } 
    
    @Override
    public void onBackPressed() {
        TicTacToeManager.getInstance().onBackKeyPress();
        super.onBackPressed();
    }
}
